<?php

/**
 * View  LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    9-feb-2012
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script src="http://malsup.github.com/jquery.form.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function() {
//
        $("span.loader").attr('style', 'visibility:hidden');
        
        $("#artist_image").rules("add", {
            accept: "png|jpe?g|gif|bmp",
            messages: {
		    accept: "<?php echo ERR_EXTENSION ?>"
            }
        });
        
        $.fn.ajaxSubmit.debug = true;
        
        var template = $("#uploaded-image-preview-box");
        
        $("#uploaded-image-preview-box").hide();
        
        $("#artist_image").change(
            function(){

            // submit the form 
            $('#artistForm').ajaxSubmit({
                url:        '/admin/ajax/upload/artistimage/artist_id/<?php echo $this->data['artist_id'] ?>', 
                beforeSubmit: function(a,f,o) {
                    o.dataType = 'json';
                    //$('#uploadOutput').html('Submitting...');
                    $(".loader").attr('style', 'visibility:visible');            
                },
                success: function(data) {
                    $(".loader").fadeOut();
                    
                    if(!data.error)
                    {
                        $("#image-upload").removeClass('error');
                        var cloned = template.clone();
                        cloned.attr('id', 'image_'+data.image_id);
                        cloned.find("img.img").attr('src', '/uploads/artists/'+data.file_name+ "?" + (new Date()).getTime());
                        cloned.find("input[type=hidden]").attr('value', data.file_name);
                        cloned.find("a.remove").attr('rel', data.image_id);
                        $("div#images").append(cloned.show());
                        
                        //$("#uploaded-image-preview-box").show();   
                        //$("#uploaded-image-preview-box img").attr('src', '/uploads/temp/'+data.file_name+ "?" + (new Date()).getTime());
                        //$("button.positive").removeAttr('disabled');
                        $("a.remove").click(function() {
                            var id = $(this).attr('rel');
                            // Invia la richiesta Ajax...
                            var jqxhr = $.post("/admin/ajax/removeimage/artistimage/id/<?php echo $this->artist_id ?>",{ image_id: id }, function(data) {
                                if(!data.error)
                                {
                                    $("div#image_"+id).remove();
                                }
                                else
                                {
                                    $("li#errorLi").show();    
                                }
                            }, "json")
                            .error(function() { $("li#errorLi").show() })
                        });                         
                    }
                    else
                    {
                        $("#image-upload").addClass('error');
                        $("#image-upload p.error").html(data.error);
                    }
                    //$out.append('<div><pre>'+ data.message +'</pre></div>');
                }
            });
            
            // return false to prevent normal browser submit and page navigation 
            return false; 
            }
        );
            
        $("a.remove").click(function() {
            var id = $(this).attr('rel');
            // Invia la richiesta Ajax...
            var jqxhr = $.post("/admin/ajax/removeimage/artistimage/id/<?php echo $this->artist_id ?>",{ image_id: id }, function(data) {
                if(!data.error)
                {
                    $("div#image_"+id).remove();
                }
                else
                {
                    $("li#errorLi").show();    
                }
            }, "json")
            .error(function() { $("li#errorLi").show() })
        });       
    });
    
    //]]>
</script>
<div class="info">
    <h2>Immagini Artisti</h2>
    <div>Per inserire un nuovo post...</div>
    <div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
</div>

<form class="fab" name="artistForm" id="artistForm" action="/admin/artisti/immagine/id/<?php echo $this->artist_id ?>" method="post" enctype="multipart/form-data">
    <ul>
        <li class="paging-context ">
            <table class="pgStyle1 circle2" cellspacing="0">
                <tr>
                    <td class="c ">
                        <var>1</var>
                    </td>
                    <td class="t">
                        <b id="pagecircle1"><a href="/admin/artisti/info/id/<?php echo $this->artist_id ?>">Info Artista</a></b>
                    </td>
                    <td class="c selected">
                        <var>2</var>
                    </td>
                    <td class="t selected">
                        <b id="pagecircle2">Immagini Artista</b>
                    </td>
                </tr>
            </table>
        </li>
    </ul>
    <div id="content">
    <ul>
	<li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none'?>">
	    <h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
                <?php var_dump( $this->errorMsg['artist_image'] )?>
	    </p>
	</li>        
        <li class="complex <?php echo $this->errorMsg['artist_image'] ? 'error' : '' ?>" id="image-upload">
            <label class="desc <?php echo $this->errorMsg['artist_image'] ? 'error' : '' ?>">Aggiungi Immagine</label>
            <div>
                <span class="input">
                    <input class="field file" type="file" name="artist_image" id="artist_image" value="" />
                </span>
            </div>
            
            <p class="error" style="<?php echo $this->errorMsg['artist_image'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['artist_image'] ?></p>
            <p id="instruct3" class="instruct">
                <small>I formati ammessi sono
                    <br/><strong>PNG</strong>, <strong>JPG</strong>, <strong>GIF</strong> e <strong>BMP</strong></small>
            </p>
        </li>
        <li>
            <span class="loader">Sto caricando...</span>
        </li>

    </ul>
        <?php if( !is_null( $this->data['artist_images'] ) ) : ?>
            <?php foreach( $this->data['artist_images'] AS $image ) : ?>
                <div class="image-preview-box" id="image_<?php echo $image['artist_image_id'] ?>">
                    <label for="actual">
                        <small><strong>Immagini</strong></small>
                        <div class="remove"><a href="#" rel="<?php echo $image['artist_image_id'] ?>" class="remove"><img title="Rimuovi immagine" src="/images/admin/cross.png" /></a></div>
                        <br/>
                        <img src="/uploads/artists/<?php echo $image['artist_image_name'] ?>" alt="Immagine Principale" title="Immagine Principale" />
                        <input type="hidden" name="artist_images[]" value="<?php echo $image['artist_image_name'] ?>" /> 
                        <br/>	
                    </label>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
        <!--  Questo Box viene nascosto al caricamento della pagina       -->
            <div id="uploaded-image-preview-box" id="" class="image-preview-box">
                <label for="new">
                    <small><strong>Immagine</strong></small>
                    <div class="remove"><a href="#" rel="" class="remove"><img title="Rimuovi immagine" src="/images/admin/cross.png" /></a></div>
                    <br/>
                    <img src="" class="img" alt="Immagine" title="Immagine" />
                    <input type="hidden" name="artist_images[]" value=""/>
                    <br/>	
                </label>
            </div>        
        
        <div id="images">

        </div>
        
        <br class="clear"/>
    </div>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <div id="formButtons">
        <button id="salva" type="submit" name="salva" value="salva" class="button positive">
            <img src="/images/admin/tick.png" alt="" />Continua
        </button>
        <button id="annulla" name="annulla" value="annulla" class="button negative">
            <img src="/images/admin/cross.png" alt="" />Annulla
        </button>
    </div>
</form>
